//
//  CircleListCommonCell.swift
//  Gaoloumi
//
//  Created by Jasper on 4/1/16.
//  Copyright © 2016 Jasper. All rights reserved.
//

import UIKit


class CircleListCommonCell: UITableViewCell {
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!
    @IBOutlet weak var imageView3: UIImageView!
    @IBOutlet weak var userLabel: UILabel!
    @IBOutlet weak var clickContLabel: UILabel!
    @IBOutlet weak var replyCountLabel: UILabel!
    @IBOutlet weak var updateTimeLabel: UILabel!
    
    @IBOutlet weak var imageHeight: NSLayoutConstraint!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    func refreshView(_ topic: Topic) {
        titleLabel.text = topic.title
        userLabel.text = topic.user?.username
        clickContLabel.text = "\(topic.clickCount)"
        replyCountLabel.text = "\(topic.replyCount)"
        
        let date = topic.object(forKey: kTopicKeyLastReplyTime) as! Date
        updateTimeLabel.text = DataCenter.sharedInstance.friendlyTime(date)

        var imgArray = [imageView1, imageView2, imageView3]
        var i = 0
        if topic.contentImage == nil || topic.contentImage?.count == 0 {
            imageHeight.constant = 0
        } else {
            let width = (UIScreen.main.bounds.width-8*4)/3
            let intWidth = Int(width)*2
            imageHeight.constant = width
            for image in topic.contentImage! {
                let urlstr = image.url
                if urlstr != nil {
                    let urlCon = "\(urlstr)?imageView/1/w/\(intWidth)/h/\(intWidth)"
                    let imageView = imgArray[i]
                    
                    let url = URL(string: urlCon)
                    imageView?.kf.setImage(with: url,
                                                 placeholder: DataCenter.sharedInstance.placeholderImage,
                                                 options: nil,
                                                 progressBlock: { receivedSize, totalSize in
                                                    
                        },
                                                 completionHandler: { image, error, cacheType, imageURL in
                                                    
                    })
                    
                    i += 1
                }
            }
        }
        
        imgArray.removeSubrange(Range(0..<i))
        for imageView in imgArray {
            imageView?.image = nil
        }
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}
